#include "../include/http.hpp"

#define WWWROOT "../http/wwwroot/"

std::string RequestRst(const Muduo::HttpRequest& req)
{
    std::stringstream ss;
    ss << req._method << " " << req._path << " " << req._version << "\r\n";
    for(auto& [a, b] : req._params)
    {
        ss << a << ": " << b << "\r\n";
    }
    for(auto& [a, b] : req._headers)
    {
        ss << a << ": " << b << "\r\n";
    }
    ss << "\r\n";
    ss << req._body;
    return ss.str();
}
//将http请求当做响应的正文响应给客户端
void Hello(const Muduo::HttpRequest& req, Muduo::HttpResponse* rsp)
{
    rsp->SetContent(RequestRst(req), "text/plain");
}
void Login(const Muduo::HttpRequest& req, Muduo::HttpResponse* rsp)
{
    rsp->SetContent(RequestRst(req), "text/plain");
}
void PutFile(const Muduo::HttpRequest& req, Muduo::HttpResponse* rsp)
{
    std::string pathname = WWWROOT + req._path;
    Muduo::Util::WriteFile(pathname, req._body);
}
void DelFile(const Muduo::HttpRequest& req, Muduo::HttpResponse* rsp)
{
    rsp->SetContent(RequestRst(req), "text/plain");
    
}
int main()
{
    Muduo::HttpServer server(8888);
    server.SetThreadCount(1);
    //设置Http服务器的web根目录
    server.SetBaseDir(WWWROOT);
    //设置功能性请求的处理函数
    server.Get("/hello", Hello);
    server.Post("/login", Login);
    server.Put("/1234.txt", PutFile);
    server.Delete("/1234.txt", DelFile);
    server.Start();
    return 0;
}